package com.ruoyi.btlfbreed.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.btlfbreed.domain.TbStockLog;
import com.ruoyi.btlfbreed.mapper.TbMedicineInfoMapper;
import com.ruoyi.btlfbreed.domain.TbMedicineInfo;
import com.ruoyi.btlfbreed.service.ITbMedicineInfoService;

/**
 * 药品信息Service业务层处理
 * 
 * @author codeman
 * @date 2024-05-04
 */
@Service
public class TbMedicineInfoServiceImpl implements ITbMedicineInfoService 
{
    @Autowired
    private TbMedicineInfoMapper tbMedicineInfoMapper;

    /**
     * 查询药品信息
     * 
     * @param id 药品信息主键
     * @return 药品信息
     */
    @Override
    public TbMedicineInfo selectTbMedicineInfoById(Long id)
    {
        return tbMedicineInfoMapper.selectTbMedicineInfoById(id);
    }

    /**
     * 查询药品信息列表
     * 
     * @param tbMedicineInfo 药品信息
     * @return 药品信息
     */
    @Override
    public List<TbMedicineInfo> selectTbMedicineInfoList(TbMedicineInfo tbMedicineInfo)
    {
        return tbMedicineInfoMapper.selectTbMedicineInfoList(tbMedicineInfo);
    }

    /**
     * 新增药品信息
     * 
     * @param tbMedicineInfo 药品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbMedicineInfo(TbMedicineInfo tbMedicineInfo)
    {
        tbMedicineInfo.setCreateTime(DateUtils.getNowDate());
        int rows = tbMedicineInfoMapper.insertTbMedicineInfo(tbMedicineInfo);

        insertTbStockLog(tbMedicineInfo);
        return rows;
    }

    /**
     * 修改药品信息
     * 
     * @param tbMedicineInfo 药品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbMedicineInfo(TbMedicineInfo tbMedicineInfo)
    {
        tbMedicineInfo.setUpdateTime(DateUtils.getNowDate());
        tbMedicineInfoMapper.deleteTbStockLogByItemId(tbMedicineInfo.getId());
        insertTbStockLog(tbMedicineInfo);
        return tbMedicineInfoMapper.updateTbMedicineInfo(tbMedicineInfo);
    }

    /**
     * 批量删除药品信息
     * 
     * @param ids 需要删除的药品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbMedicineInfoByIds(Long[] ids)
    {
        tbMedicineInfoMapper.deleteTbStockLogByItemIds(ids);
        return tbMedicineInfoMapper.deleteTbMedicineInfoByIds(ids);
    }

    /**
     * 删除药品信息信息
     * 
     * @param id 药品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbMedicineInfoById(Long id)
    {
        tbMedicineInfoMapper.deleteTbStockLogByItemId(id);
        return tbMedicineInfoMapper.deleteTbMedicineInfoById(id);
    }

    /**
     * 新增备品出入库信息
     * 
     * @param tbMedicineInfo 药品信息对象
     */
    public void insertTbStockLog(TbMedicineInfo tbMedicineInfo)
    {
        List<TbStockLog> tbStockLogList = tbMedicineInfo.getTbStockLogList();
        Long id = tbMedicineInfo.getId();
        if (StringUtils.isNotNull(tbStockLogList))
        {
            List<TbStockLog> list = new ArrayList<TbStockLog>();
            for (TbStockLog tbStockLog : tbStockLogList)
            {
                tbStockLog.setItemId(id);
                list.add(tbStockLog);
            }
            if (list.size() > 0)
            {
                tbMedicineInfoMapper.batchTbStockLog(list);
            }
        }
    }
}
